package com.ny.controller;

import com.ny.entity.Device;
import com.ny.entity.Result;
import com.ny.mapper.DeviceMapper;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    // 接口：http://localhost:8083/device?name=客厅主灯
    @GetMapping("/findAll")
    public Result findAll(){
        return Result.success(deviceService.findAll());
    }
    // 支持您原来的URL格式：http://localhost:8081/device?name=客厅主灯
    @GetMapping
    public Result findDeviceByName(@RequestParam String name) {
        List<Device> devices = deviceService.findByDeviceName(name);
        return Result.success(devices);
    }

    // 精确查询：http://localhost:8081/device/findByName?name=客厅主灯
    @GetMapping("/findByName")
    public Result findByName(@RequestParam String name) {
        List<Device> devices = deviceService.findByDeviceName(name);
        return Result.success(devices);
    }

    // 模糊查询：http://localhost:8081/device/search?keyword=空调
    @GetMapping("/search")
    public Result search(@RequestParam String keyword) {
        List<Device> devices = deviceService.findByDeviceNameContaining(keyword);
        return Result.success(devices);
    }

    // 测试接口：检查数据连接
    @GetMapping("/test")
    public Result testConnection() {
        List<Device> allDevices = deviceService.findAll();
        return Result.success("总设备数: " + allDevices.size());
    }

    // 调试接口：查看所有数据
    @GetMapping("/debug")
    public Result debug() {
        List<Device> allDevices = deviceService.findAll();
        return Result.success(allDevices);
    }
    @GetMapping("/findByTimeAndStatus")
    public Result findByTimeAndStatus(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            @RequestParam Integer status) {

        List<Device> devices = deviceService.findByTimeRangeAndStatus(startTime, endTime, status);
        return Result.success(devices);
    }
    // 接口：http://localhost:8081/device/findByStatus?status=0
    @GetMapping("/findByStatus")
    public Result findByStatus(@RequestParam Integer status) {
        List<Device> devices = deviceService.findByStatus(status);
        return Result.success(devices);
    }
    @GetMapping("/query")
    public Result queryDevices(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            @RequestParam Integer status) {

        List<Device> devices;
        if (startTime != null && endTime != null) {
            // 按时间段和状态查询
            devices = deviceService.findByTimeRangeAndStatus(startTime, endTime, status);
        } else {
            // 只按状态查询
            devices = deviceService.findByStatus(status);
        }

        return Result.success(devices);
    }
}